<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class News_mdl extends MY_Model {

    public function __construct(){
        parent::__construct();
        $this->table = 'news';
    }

    //消息内容
    public $_type = array(
        '1' => '系统公告',
        '2' => '系统提示'
    );


    /**
     * 获取新消息
     * @return [type] [description]
     */
    public function get_news(){
        $this->db->where('user_id',$_SESSION['user']['id']);
        $this->db->where('status','1');
        return $this->get_count();
    }
	
	/**
	 * 发送信息给用户
	 * $uid   a 发送给全部用户 g 发送给所有管理员 y 发送给所有用户 id 发送给单个用户
	 */
	public function send_user($uid,$con,$type = 2){
		//组装信息
		$data = [
			'type' => $type,
			'content' => $con,
			'status' => 1, 
			'create_time' => $_SERVER['REQUEST_TIME']
		];
		
		if(is_numeric($uid) && !empty($uid)){
			$data['user_id'] = $uid;
			return $this->create($data);
		}
		
		$this->load->model('user_mdl');
		$this->db->select('id');
		
		//全部
		if($uid == 'a'){
			$all = $this->user_mdl->select();
			foreach($all as $k => $v){
				$data['user_id'] = $v['id'];
				$this->create($data);
			}
			return true;
		}
		
		//管理员
		if($uid == 'g'){
			$this->db->where('group',1);
			$this->db->or_where('group',2);
			$all = $this->user_mdl->select();
			foreach($all as $k => $v){
				$data['user_id'] = $v['id'];
				$this->create($data);
			}
			return true;
		}
		
		//所有用户
		if($uid == 'y'){
			$this->db->where('group',3);
			$this->db->or_where('group',4);
			$all = $this->user_mdl->select();
			foreach($all as $k => $v){
				$data['user_id'] = $v['id'];
				$this->create($data);
			}
			return true;
		}
		
		return false;
	}


}

/* End of file News_mdl.php */
/* Location: ./application/models/News_mdl.php */